package com.pzhu.gmall.manage.service.impl;

import java.util.List;

import com.pzhu.gmall.bean.*;
import com.pzhu.gmall.manage.mapper.PmsProductImageMapper;
import com.pzhu.gmall.manage.mapper.PmsProductInfoMapper;
import com.pzhu.gmall.manage.mapper.PmsProductSaleAttrMapper;
import com.pzhu.gmall.manage.mapper.PmsProductSaleAttrValueMapper;
import com.pzhu.gmall.service.SpuService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 刘鹏 liupeng
 * @date 2019/10/31
 **/
@Service
public class SpuServiceImpl implements SpuService {
    @Autowired
    private PmsProductInfoMapper pmsProductInfoMapper;
    @Autowired
    private PmsProductImageMapper pmsProductImageMapper;
    @Autowired
    private PmsProductSaleAttrMapper pmsProductSaleAttrMapper;

    @Autowired
    private PmsProductSaleAttrValueMapper pmsProductSaleAttrValueMapper;

    @Override
    public List<PmsProductInfo> getSpuListBycatalog3Id(String catalog3Id) {
        PmsProductInfo pmsProductInfo = new PmsProductInfo();
        pmsProductInfo.setCatalog3Id(catalog3Id);
        return pmsProductInfoMapper.select(pmsProductInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(PmsProductInfo pmsProductInfo) {
        if (pmsProductInfo.getId() != null) {
            pmsProductInfoMapper.updateByPrimaryKey(pmsProductInfo);
        } else {
            pmsProductInfoMapper.insertSelective(pmsProductInfo);
        }
        //save
        List<PmsProductImage> spuImageList = pmsProductInfo.getSpuImageList();
        List<PmsProductSaleAttr> spuSaleAttrList = pmsProductInfo.getSpuSaleAttrList();
        for (PmsProductImage pmsProductImage : spuImageList) {
            pmsProductImage.setProductId(pmsProductInfo.getId());
            pmsProductImageMapper.insert(pmsProductImage);
        }
        for (PmsProductSaleAttr pmsProductSaleAttr : spuSaleAttrList) {
            pmsProductSaleAttr.setProductId(pmsProductInfo.getId());
            pmsProductSaleAttrMapper.insert(pmsProductSaleAttr);
            List<PmsProductSaleAttrValue> pmsProductSaleAttrValueList = pmsProductSaleAttr.getSpuSaleAttrValueList();
            for (PmsProductSaleAttrValue pmsProductSaleAttrValue : pmsProductSaleAttrValueList) {
                pmsProductSaleAttrValue.setProductId(pmsProductInfo.getId());
                pmsProductSaleAttrValue.setSaleAttrId(pmsProductSaleAttr.getSaleAttrId());
                pmsProductSaleAttrValueMapper.insert(pmsProductSaleAttrValue);
            }
        }

    }

    @Override
    public List<PmsProductSaleAttr> getSpuSaleAttribute(String productId, String itemId) {
//        PmsProductSaleAttr pmsProductSaleAttr = new PmsProductSaleAttr();
//        pmsProductSaleAttr.setProductId(productId);
//        List<PmsProductSaleAttr> pmsProductSaleAttrList = pmsProductSaleAttrMapper.select(pmsProductSaleAttr);
//        for (PmsProductSaleAttr productSaleAttr : pmsProductSaleAttrList) {
//            PmsProductSaleAttrValue pmsProductSaleAttrValue = new PmsProductSaleAttrValue();
//            pmsProductSaleAttrValue.setProductId(productId);
//            pmsProductSaleAttrValue.setSaleAttrId(productSaleAttr.getSaleAttrId());
//            List<PmsProductSaleAttrValue> pmsProductSaleAttrValueList =
//                    pmsProductSaleAttrValueMapper.select(pmsProductSaleAttrValue);
//            productSaleAttr.setSpuSaleAttrValueList(pmsProductSaleAttrValueList);
//        }
//        return pmsProductSaleAttrList;
        return pmsProductSaleAttrMapper.selectByproductId(productId, itemId);
    }

    @Override
    public List<SkuHash> getSkuSHash(String spuId) {
        return pmsProductSaleAttrMapper.selectSkuHash(spuId);
    }
}
